package com.sm.controller;

import com.alipay.api.domain.Injured;
import com.sm.bo.VipOrderBO;
import com.sm.model.ApplyTraditional;
import com.sm.model.MyVip;
import com.sm.model.User;
import com.sm.model.VipDictionary;
import com.sm.service.*;
import com.sm.util.Msg;
import com.sm.wx.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vipOrder")
public class VipOrderController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserVipOrderService userVipOrderService;

    @Autowired
    private VipDictionaryService vipDictionaryService;

    @Autowired
    private MyVipService myVipService;

    @Autowired
    private IncomeDetailService incomeDetailService;

    @Autowired
    private ApplyTraditionalService applyTraditionalService;

    /**
     * 创建VIP订单
     *
     * @param vipOrder
     * @return
     */
    @RequestMapping("/create")
    @ResponseBody
    public Msg createOrder(HttpServletRequest request,VipOrderBO vipOrder) {
        String msg;
        User user = userService.selectUserById(vipOrder.getUserId());
        List<Long> vipIds = vipOrder.getVipIds();
        //应该是多少钱
        BigDecimal price = new BigDecimal(0);
        if (vipOrder.getUserId() == null || user == null) {
            msg = "用户不存在";
        } else if (vipOrder.getPayType() != 1 && vipOrder.getPayType() != 2) {
            msg = "请选择支付方式";
        } else if (vipIds == null || vipIds.size() == 0) {
            msg = "请选择要开通的会员";
        } else {
            List<String> idsStr = new ArrayList<>();
            for (Long vipId : vipIds) {
                if (vipId < 0 || vipId > 6) {
                    msg = "选择的会员不正确";
                    return Msg.fail().add("msg", msg);
                }
                VipDictionary vipDictionary = vipDictionaryService.getById(vipId);
                price = price.add(vipDictionary.getPrice());
                idsStr.add(vipId.toString());
            }
            vipOrder.setVipIdStrs(idsStr);
            if (vipOrder.getPrice().compareTo(price) != 0) {
                msg = "价格异常";
            } else {
                AjaxResult order = userVipOrderService.createVipOrder(request,vipOrder);
                return Msg.success(order);
            }
        }
        return Msg.fail().add("msg", msg);
    }


    /**
     * 获取我的VIP列表
     * @param userId
     * @return
     */
    @RequestMapping("/getMyVipList")
    @ResponseBody
    public Msg getMyVipList(@RequestParam("userId") Integer userId) {
        List<MyVip> myVip = myVipService.getMyVip(userId);
        if (myVip == null || myVip.size() == 0) {
            myVip = new ArrayList<>();
            myVip.add(new MyVip(null, userId, null, "普通会员", BigDecimal.ZERO, null, null, null, 0));
        } else {
            for (MyVip vip : myVip) {
                if (vip.getVipId().equals(6L)) {
                    List<MyVip> myVipList = myVipService.getMyVipList(userId);
                    List<String> vipNamelist = new ArrayList<>();
                    vipNamelist.add("创意美术");
                    vipNamelist.add("传媒口才");
                    vipNamelist.add("助梦舞蹈");
                    for (MyVip myVip1 : myVipList) {
                        if (myVip1.getVipId().equals(1L)) {
                            vipNamelist.add("国学启蒙");
                        }
                        if(myVip1.getVipId().equals(2L)) {
                            vipNamelist.add("潜能开发");
                        }
                    }
                    vip.setVipNames(vipNamelist);
                }
            }
        }

        User user = userService.selectByPrimaryKey(userId);
        Map<String, Object> result = new HashMap<>();
        result.put("myVipList", myVip);
        result.put("nickName", user.getNickName());
        result.put("headLink", user.getHeadLink());
        result.put("commission", incomeDetailService.getSumByUserId(userId));
        result.put("recommendCount", incomeDetailService.getUserNumByUserId(userId));
        return Msg.success(result);
    }
    /**
     * 获取开通国学启蒙和潜能开发状态
     * @param userId
     * @return
     */
    @RequestMapping("/getTwoVipStatus")
    @ResponseBody
    public Msg getTwoVipStatus(@RequestParam("userId") Integer userId) {
        List<Integer> myVips = myVipService.getMyVipIdList(userId);
        //啥都没有开通
        int status = 0;
        //啥都没申请
        int appling = 0;
        if (myVips != null) {
            //1国学启蒙
            if (myVips.contains(1)) {
                status = 1;
            }
            if (myVips.contains(2)) {
                status = 2;
            }
            if (myVips.contains(1) && myVips.contains(2)) {
                status = 3;
            }
        }

        //申请中的国学启蒙
        ApplyTraditional applyTraditional = applyTraditionalService.selectByUserIdAndType(userId, 1);
        //申请中的潜能开发
        ApplyTraditional applyTraditionalQ = applyTraditionalService.selectByUserIdAndType(userId, 2);
        if (applyTraditional != null) {
            appling = 1;
        }
        if (applyTraditionalQ != null) {
            appling = 2;
        }
        if (applyTraditionalQ != null && applyTraditional != null) {
            appling = 3;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("appling", appling);
        return Msg.success(map);
    }





}
